//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.tuiofx.internal.gesture;

import javafx.application.Platform;
import javafx.geometry.Point2D;
import javafx.scene.Node;
import javafx.scene.input.PickResult;
import javafx.scene.input.ZoomEvent;
import org.tuiofx.Configuration;

public class ZoomGestureRecognizer extends GestureRecognizer {
    private ZoomingData zoomingData;
    private boolean isZooming = false;
    private float minSpan = 8.0E-4F;
    private float startSpan = 7.5E-4F;
    private double totalZoomFactor = 1.0;

    public ZoomGestureRecognizer(Node targetNode, Configuration config) {
        super(targetNode, config);
        this.requiredNumberOfTouches = 2;
        this.zoomingData = new ZoomingData();
    }

    private Point2D getAverageDeviation() {
        int div = Math.min(this.touchPointsCount, this.maxNumberOfTouches);
        Point2D centroid = this.getCentroid();
        float devSumX = 0.0F;
        float devSumY = 0.0F;

        for (int i = 0; i < this.touchPointsCount && i < this.maxNumberOfTouches; ++i) {
            devSumX = (float) ((double) devSumX + Math.abs(((TuioTouchPoint) this.touchPointsList.get(i)).getSceneX() - centroid.getX()));
            devSumY = (float) ((double) devSumY + Math.abs(((TuioTouchPoint) this.touchPointsList.get(i)).getSceneY() - centroid.getY()));
        }

        float devX = devSumX / (float) div;
        float devY = devSumY / (float) div;
        return new Point2D((double) devX, (double) devY);
    }

    public void touchesBegan(TuioTouchPoint touchPoint) {
        if (this.getTargetNode() != null && this.touchPointsCount >= this.requiredNumberOfTouches) {
            Point2D avgDev = this.getAverageDeviation();
            float spanX = (float) avgDev.getX() * 2.0F;
            float spanY = (float) avgDev.getY() * 2.0F;
            float span = (float) Math.sqrt(spanX * spanX + spanY * spanY);
            if (!this.isZooming) {
                this.zoomingData.setInitialSpan(span);
                this.zoomingData.setCurrSpanX(spanX);
                this.zoomingData.setCurrSpanY(spanY);
                this.zoomingData.setPrevSpan(span);
                this.zoomingData.setCurrSpan(span);
            }

        }
    }

    public void touchesMove(final TuioTouchPoint touchPoint) {
        if (this.getTargetNode() != null && this.touchPointsCount >= this.requiredNumberOfTouches) {
            Point2D avgDev = this.getAverageDeviation();
            float spanX = (float) avgDev.getX() * 2.0F;
            float spanY = (float) avgDev.getY() * 2.0F;
            float span = (float) Math.sqrt(spanX * spanX + spanY * spanY);
            if (!this.isZooming && Math.abs(span - this.zoomingData.getPrevSpan()) > this.startSpan) {
                Platform.runLater(new Runnable() {
                    public void run() {
                        new PickResult(touchPoint.getTargetNode(), touchPoint.getSceneX(), touchPoint.getSceneY());
                        ZoomGestureRecognizer.this.fireEvent(touchPoint.getTargetNode(), new ZoomEvent(ZoomEvent.ZOOM_STARTED, touchPoint.getScreenX(), touchPoint.getScreenY(), touchPoint.getScreenX(), touchPoint.getScreenY(), false, false, false, false, true, false, 1.0, 1.0, (PickResult) null));
                    }
                });
                this.isZooming = true;
            }

            boolean performZooming = Math.abs(span - this.zoomingData.getPrevSpan()) > this.minSpan;
            this.zoomingData.setPrevSpan(this.zoomingData.getCurrSpan());
            this.zoomingData.setCurrSpan(span);
            this.zoomingData.setCurrSpanX(spanX);
            this.zoomingData.setCurrSpanY(spanY);
            if (performZooming) {
                final double x = touchPoint.getSceneX();
                final double y = touchPoint.getSceneY();
                final double screenX = touchPoint.getScreenX();
                final double screenY = touchPoint.getScreenY();
                final boolean _shiftDown = false;
                final boolean _controlDown = false;
                final boolean _altDown = false;
                final boolean _metaDown = false;
                final boolean _direct = true;
                final boolean _inertia = false;
                final double zoomFactor = this.zoomingData.getPrevSpan() > 0.0F ? (double) (this.zoomingData.getCurrSpan() / this.zoomingData.getPrevSpan()) : 1.0;
                this.totalZoomFactor *= zoomFactor;
                Platform.runLater(new Runnable() {
                    public void run() {
                        PickResult pickResult = new PickResult(touchPoint.getTargetNode(), touchPoint.getSceneX(), touchPoint.getSceneY());
                        ZoomGestureRecognizer.this.fireEvent(touchPoint.getTargetNode(), new ZoomEvent(ZoomEvent.ZOOM, x, y, screenX, screenY, _shiftDown, _controlDown, _altDown, _metaDown, _direct, _inertia, zoomFactor, ZoomGestureRecognizer.this.totalZoomFactor, pickResult));
                    }
                });
            }

        }
    }

    public void touchesEnd(final TuioTouchPoint touchPoint) {
        if (this.isZooming && !this.touchPointsList.isEmpty()) {
            if (this.touchPointsList.size() == 1) {
                this.isZooming = false;
                Platform.runLater(new Runnable() {
                    public void run() {
                        ZoomGestureRecognizer.this.fireEvent(touchPoint.getTargetNode(), new ZoomEvent(ZoomEvent.ZOOM_FINISHED, touchPoint.getSceneX(), touchPoint.getSceneY(), touchPoint.getScreenX(), touchPoint.getScreenY(), false, false, false, false, true, false, 1.0, ZoomGestureRecognizer.this.totalZoomFactor, (PickResult) null));
                    }
                });
                this.totalZoomFactor = 1.0;
            }

        }
    }

    private static class ZoomingData {
        private float initialSpan;
        private float mCurrSpanX;
        private float mCurrSpanY;
        private float mPrevSpan;
        private float mCurrSpan;

        ZoomingData() {
        }

        public float getInitialSpan() {
            return this.initialSpan;
        }

        public void setInitialSpan(float mInitialSpan) {
            this.initialSpan = mInitialSpan;
        }

        public float getCurrSpanX() {
            return this.mCurrSpanX;
        }

        public void setCurrSpanX(float mCurrSpanX) {
            this.mCurrSpanX = mCurrSpanX;
        }

        public float getCurrSpanY() {
            return this.mCurrSpanY;
        }

        public void setCurrSpanY(float mCurrSpanY) {
            this.mCurrSpanY = mCurrSpanY;
        }

        public float getPrevSpan() {
            return this.mPrevSpan;
        }

        public void setPrevSpan(float mPrevSpan) {
            this.mPrevSpan = mPrevSpan;
        }

        public float getCurrSpan() {
            return this.mCurrSpan;
        }

        public void setCurrSpan(float mCurrSpan) {
            this.mCurrSpan = mCurrSpan;
        }
    }
}
